<template>
  <div class="app-container">
    <!-- 保养单 -->
    <!-- 查询和其他操作 -->
    <el-form :inline="true" class="demo-form-inline filter">
      <div>
        <el-form-item label="保养日期">
          <el-date-picker
            v-model="alarmdate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"/>
        </el-form-item>
        <el-form-item label="保养单号">
          <el-input
            v-model="listQuery.fbillno"
            clearable
            class="filter-item"
            style="width: 200px"
            placeholder="请输入保养单号"
          />
        </el-form-item>
        <el-form-item label="保养状态">
          <el-select
            v-model="listQuery.fbillstate"
            clearable
            filterable
            class="filter-item"
            placeholder="请选择保养状态"
          >
            <el-option
              v-for="item in siteTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </div>
      <div>
        <span v-if="this.$store.state.user.fsiteLevel==3" >
          <el-form-item label="客户名称">
            <el-select v-model="listQuery.fsiteNo" disabled clearable filterable class="filter-item" placeholder="请选择客户名称">
              <el-option
                v-for="item in getListfsiteNo"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="保养人员">
            <div style="width: 220px;display:flex;justify-content:flex-end;align-items:center">
              <el-select v-model="listQuery.fmmuser" style="display: inline-block;width:200px" clearable filterable class="filter-item" placeholder="请选择保养人员">
                <el-option
                  v-for="item in getListfmmuser"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoguse('search')"/>
            </div>
          </el-form-item>
          <el-form-item label="保养地点">
            <div style="width: 220px;display:flex;justify-content:flex-end;align-items:center">
              <el-select v-model="listQuery.fpointNo" style="display: inline-block;width:200px" clearable filterable class="filter-item" placeholder="请选择巡查地点">
                <el-option
                  v-for="item in getListfpointNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoglab('search')"/>
            </div>
          </el-form-item>
        </span>
        <span v-else>
          <el-form-item label="客户名称">
            <el-select v-model="listQuery.fsiteNo" clearable filterable class="filter-item" placeholder="请选择客户名称" @change="changeFsiteNo">
              <el-option
                v-for="item in getListfsiteNo"
                :key="item.value"
                :label="item.label"
                :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item label="保养人员">
            <div style="width: 220px;display:flex;justify-content:flex-end;align-items:center">
              <el-select v-model="listQuery.fmmuser" style="display: inline-block;width:200px" clearable filterable class="filter-item" placeholder="请选择保养人员">
                <el-option
                  v-for="item in getListfmmuserArea"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoguse('search')"/>
            </div>
          </el-form-item>
          <el-form-item label="保养地点">
            <div style="width: 220px;display:flex;justify-content:flex-end;align-items:center">
              <el-select v-model="listQuery.fpointNo" style="display: inline-block;width:200px" clearable filterable class="filter-item" placeholder="请选择巡查地点">
                <el-option
                  v-for="item in getListfpointNoArea"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <i style="display: inline-block;width:20px;color:#409EFF;margin-left:3px" class="el-icon-search" @click="searchDialoglab('search')"/>
            </div>
          </el-form-item>
        </span>
      </div>
      <el-form-item label="保养类型">
        <el-select v-model="listQuery.fmmtypeno" filterable clearable class="filter-item" placeholder="请选择保养类型">
          <el-option
            v-for="item in getListfmmtypeno"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <!-- v-permission="['maintain:mmHead:read']" -->
        <el-button
          :style="{background:buttonColor.lookup,color:buttonColor.color}"
          round
          size="medium"
          class="filter-item"
          @click="handleFilter"
        >查找</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 查询结果 -->
    <el-table
      v-loading="listLoading"
      :data="list"
      :header-cell-style="tableColor"
      stripe
      element-loading-text="正在查询中。。。"
      border
      fit
      highlight-current-row
    >
      <el-table-column :width="buttonColor.operationTwo" align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <div class="operationButton">
            <div :style="{color:buttonColor.edit,cursor:'pointer'}" @click="handleUpdate(scope.row)">
              <img src="../../../public/images/edit.png" >
              查看
            </div>

          </div>
        </template>
      </el-table-column>

      <el-table-column
        :index="indexMethod"
        align="center"
        label="序"
        type="index"
      />
      <el-table-column align="center" label="保养单号" prop="fbillno" width="180"/>
      <el-table-column v-if="this.$store.state.user.fsiteLevel!=3" :formatter="formatfsiteNo" width="150" align="center" label="客户名称" prop="fsiteNo" />
      <el-table-column :formatter="formatfpointNo" align="center" label="保养地点" prop="fpointNo" />
      <el-table-column :formatter="formatfmmuser" align="center" label="保养人员" prop="fmmuser" />
      <el-table-column
        :formatter="datetimeFormat"
        width="100"
        align="center"
        label="保养日期"
        prop="fmmdate"
      />

      <el-table-column
        width="130"
        align="center"
        label="固定资产码"
        prop="fequipmentbarcode"
      />
      <el-table-column align="center" label="设备名称" prop="fequipmenttag" />
      <el-table-column :formatter="formatfmmtypeno" align="center" label="保养类型" prop="fmmtypeno" />
      <el-table-column :formatter="formatfmmlevelno" align="center" label="保养级别" prop="fmmlevelno" />
      <el-table-column align="center" label="保养工时" prop="fmmworktime" />
      <el-table-column align="center" label="单据状态" prop="fbillstate">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.fbillstate | siteTypeFilter }}</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      width="auto"
    >
      <template slot="title">
        <div :style="titleColor">{{ textMap[dialogStatus] }}</div>
      </template>
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="dataForm"
        status-icon
        label-position="left"
        label-width="auto"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="保养单号" prop="fbillno">
              <el-input
                :disabled="true"
                v-model="dataForm.fbillno"
            /> </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item label="保养日期" prop="fmmdate">
              <el-date-picker
                :disabled="true"
                v-model="dataForm.fmmdate"
                type="date"
            /> </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="单据状态"
              prop="fbillstate"
            >
              <el-select
                :disabled="true"
                v-model="dataForm.fbillstate"
                class="filter-item"
                placeholder="请选择状态"
              >
                <el-option
                  v-for="item in siteTypeList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
            </el-select> </el-form-item
            >
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item
              v-if="this.$store.state.user.fsiteLevel==3"
              label="客户名称"
              prop="fsiteNo"

            >
              <el-select v-model="dataForm.fsiteNo" disabled clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
            <el-form-item
              v-else
              label="客户名称"
              prop="fsiteNo"
            >
              <el-select v-if="dialogStatus=='create'" v-model="dataForm.fsiteNo" clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
              <el-select v-else v-model="dataForm.fsiteNo" disabled clearable filterable class="filter-item" placeholder="请选择客户名称">
                <el-option
                  v-for="item in getListfsiteNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="保养地点"
              prop="fpointNo"
            >
              <el-select v-model="dataForm.fpointNo" disabled style="display: inline-block" clearable filterable class="filter-item" placeholder="请选择保养地点">
                <el-option
                  v-for="item in getListfpointNo"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item label="保养人员" prop="fmmuser">
              <el-select v-model="dataForm.fmmuser" disabled style="display: inline-block" clearable filterable class="filter-item" placeholder="请选择保养人员">
                <el-option
                  v-for="item in getListfmmuser"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item
            >
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item
              label="固定资产码"
              prop="fequipmentbarcode"
            >
            <el-input :disabled="true" v-model="dataForm.fequipmentbarcode" /> </el-form-item
            >

          </el-col>
          <el-col :span="8">
            <el-form-item
              label="设备名称"
              prop="fequipmenttag"
            >
            <el-input v-model="dataForm.fequipmenttag" disabled /> </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="保养工时"
              prop="fmmworktime"
            >
            <el-input :disabled="true" v-model="dataForm.fmmworktime" /> </el-form-item
            >
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item
              label="保养类型"
              prop="fmmtypeno"
            >
              <el-select v-model="dataForm.fmmtypeno" disabled filterable clearable class="filter-item" placeholder="请选择保养类型">
                <el-option
                  v-for="item in getListfmmtypeno"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
            </el-select> </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="保养级别"
              prop="fmmlevelno"
            >
              <el-select v-model="dataForm.fmmlevelno" disabled filterable clearable class="filter-item" placeholder="请选择保养级别">
                <el-option
                  v-for="item in getListfmmlevelno"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
            </el-select> </el-form-item
            >
          </el-col>
          <el-col :span="8">
            <el-form-item label="完成日期" prop="fupddate">
              <el-date-picker
                :disabled="true"
                v-model="dataForm.fupddate"
                type="date"
            /> </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-tabs type="border-card">
        <el-tab-pane label="保养列表">
          <div class="app-container">
            <!-- 子表查询结果 -->
            <el-table
              v-loading="listLoading"
              :data="dataForm.details"
              :header-cell-style="tableColor"
              stripe
              element-loading-text="正在查询中。。。"
              border
              fit
              highlight-current-row
              height="20vh"
            >
              <el-table-column
                :index="indexMethod"
                align="center"
                label="序"
                type="index"
              />
              <el-table-column
                :formatter="fmmitemnoFormat"
                align="center"
                label="保养项目"
                prop="fmmitemno"
              />
              <el-table-column align="center" label="操作方式" prop="fmmflag" />
              <el-table-column
                align="center"
                label="操作结果"
                prop="fmmresult"
              />
              <el-table-column align="center" label="备注" prop="fmemo" />
            </el-table>
          </div>
        </el-tab-pane>
        <el-tab-pane label="保养记录">
          <el-input
            :rows="9"
            v-model="dataForm.fexplanation"
            disabled
            type="textarea"
            placeholder="暂无内容"
          />
        </el-tab-pane>
        <el-tab-pane label="保养照片">
          <div class="app-container">
            <!-- 子表二查询结果 -->
            <el-table
              v-loading="listLoading"
              :data="dataForm.details2"
              :header-cell-style="tableColor"
              stripe
              element-loading-text="正在查询中。。。"
              border
              fit
              highlight-current-row
              height="20vh"
            >
              <el-table-column
                :index="indexMethod"
                align="center"
                label="序"
                type="index"
              />
              <el-table-column align="center" label="类型" prop="fflag" />
              <el-table-column align="center" label="照片" prop="fimage">
                <template slot-scope="scope">
                  <img :src="scope.row.fimage" class="avatar">
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">确定</el-button>
      </div>
    </el-dialog>
    <userDialog :search-visibleuse.sync="searchVisibleuse" :fsite-no.sync="userDialogFsiteNo" :table-color="tableColor" @userDialog="userDialogValue" />
    <labDialog :search-visiblelab.sync="searchVisiblelab" :fsite-no.sync="labDialogFsiteNo" :table-color="tableColor" @labDialog="labDialogValue" />
  </div>
</template>

<style>
.button-margin {
  margin: 2px 0px;
}
.el-button + .el-button {
  margin-left: 1px;
}
</style>

<script>
import { listmmHead } from '@/api/mmHead'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import { tableColor, titleColor, buttonColor } from '@/filters/index'
import { getListArea, fsiteOptionsByLevel } from '@/api/area'
import UserDialog from '@/components/UserDialog'
import LabDialog from '@/components/LabDialog'

const siteTypeMap = {
  1: '计划中',
  2: '已保养',
  8: '已取消'
}

export default {
  name: 'STcMmHeadSingle',
  components: { Pagination, UserDialog, LabDialog },
  filters: {
    siteTypeFilter(type) {
      return siteTypeMap[type]
    }
  },
  data() {
    return {
      buttonColor: buttonColor,
      flineList: [],
      selectList: [],
      getListfpointNoArea: null,
      getListfmmuserArea: null,
      labFlag: null,
      searchVisiblelab: false,
      labDialogFsiteNo: null,
      searchVisibleuse: false,
      userDialogFsiteNo: null,
      userFlag: null,

      getListfsiteNo: [],
      getListfmmitemno: null,
      getListfmmuser: null,
      getListfmmtypeno: [],
      getListfmmlevelno: [],
      getListfpointNo: null,
      list: null,
      total: 0,
      listLoading: true,
      alarmdate: null,
      listQuery: {
        page: 1,
        limit: 20,
        fmmdate: undefined,
        ftakedateStart: undefined,
        ftakedateEnd: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fequipmentbarcode: undefined,
        fequipmenttag: undefined,
        feuipmentid: undefined,
        fmmtypeno: undefined,
        fmmlevelno: undefined,
        fmmworktime: undefined,
        fmmuser: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      siteTypeList: [
        { value: 1, label: '计划中' },
        { value: 2, label: '已保养' },
        { value: 8, label: '已取消' }
      ],
      dataForm: {
        fmmdate: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fequipmentbarcode: undefined,
        fequipmenttag: undefined,
        feuipmentid: undefined,
        fmmtypeno: undefined,
        fmmlevelno: undefined,
        fmmworktime: undefined,
        fmmuser: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        id: undefined,
        details: [],
        details2: [],
        details3: undefined
      },
      actionsTmp3: undefined,
      actionsTmp: [],
      dialogFormVisible1: false,
      dialogStatus1: '',
      dataForm1: {},

      actionsTmp2: [],
      dialogFormVisible2: false,
      dialogStatus2: '',
      dataForm2: {},

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      tableColor: tableColor,
      titleColor: titleColor,
      rules: {
        fmmdate: [{ required: true, message: '保养日期不能为空' }],
        fsiteNo: [{ required: true, message: '客户编号不能为空' }],
        fpointNo: [{ required: true, message: '教室编号不能为空' }],
        fequipmentbarcode: [{ required: true, message: '固定资产码不能为空' }],
        feuipmentid: [
          { required: true, message: '设备编号不能为空' },
          {
            type: 'number',
            message: '设备编号必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fmmtypeno: [
          { required: true, message: '保养类型不能为空' },
          {
            type: 'number',
            message: '保养类型必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fmmlevelno: [
          { required: true, message: '保养级别不能为空' },
          {
            type: 'number',
            message: '保养级别必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fmmworktime: [],
        fmmuser: [{ required: true, message: '工程师不能为空' }],
        fexplanation: [],
        fupddate: [],
        foperator: [],
        fcheckdate: [],
        fcheckuser: [],
        fbillstate: [
          {
            type: 'number',
            message: '单据状态必须为数字值',
            transform: (value) => Number(value)
          }
        ]
      },
      rules1: {
        fline: [
          { required: true, message: '行号不能为空' },
          {
            type: 'number',
            message: '行号必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fmmitemno: [{ required: true, message: '保养项目不能为空' }],
        fmmflag: [
          {
            type: 'number',
            message: '操作方式必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fmmresult: [],
        fmemo: []
      },
      rules2: {
        fline: [
          { required: true, message: '行号不能为空' },
          {
            type: 'number',
            message: '行号必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fflag: [
          { required: true, message: '类型不能为空' },
          {
            type: 'number',
            message: '类型必须为数字值',
            transform: (value) => Number(value)
          }
        ],
        fimage: [{ required: true, message: '照片不能为空' }]
      }
    }
  },
  created() {
    this.alarmdate = [this.parserDate(moment(new Date().setDate(1)).format('YYYY-MM-DD')), new Date()]
    this.getList()
    fsiteOptionsByLevel(3)
      .then(response => {
        this.getListfsiteNo = response.data.data.list
      })
      .catch(() => {
        this.getListfsiteNo = []
      })
    if (this.$store.state.user.fsiteLevel == 3) {
      this.listQuery.fsiteNo = this.$store.state.user.fsiteNo
      // 地点
      getListArea({ flag: 10, arg1: this.$store.state.user.fsiteNo })
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
        // 人员弹出框
      getListArea({ flag: 13, arg1: this.$store.state.user.fsiteNo })
        .then(response => {
          this.getListfmmuser = response.data.data.list
        })
        .catch(() => {
          this.getListfmmuser = []
        })
    } else {
      // 地点
      getListArea({ flag: 10 })
        .then(response => {
          this.getListfpointNo = response.data.data.list
        })
        .catch(() => {
          this.getListfpointNo = []
        })
        // 人员弹出框
      getListArea({ flag: 13 })
        .then(response => {
          this.getListfmmuser = response.data.data.list
        })
        .catch(() => {
          this.getListfmmuser = []
        })
    }

    getListArea({ flag: 17 })
      .then(response => {
        this.getListfmmlevelno = response.data.data.list
      })
      .catch(() => {
        this.getListfmmlevelno = []
      })

    getListArea({ flag: 16 })
      .then(response => {
        this.getListfmmtypeno = response.data.data.list
      })
      .catch(() => {
        this.getListfmmtypeno = []
      })
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD')
    },
    parserDate(date) {
      var t = Date.parse(date)
      if (!isNaN(t)) {
        return new Date(Date.parse(date.replace(/-/g, '/')))
      }
    },
    formatfsiteNo: function(row, column) {
      if (this.getListfsiteNo) {
        for (let i = 0; i < this.getListfsiteNo.length; i++) {
          if (row.fsiteNo === this.getListfsiteNo[i].value) {
            return this.getListfsiteNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },

    changeFsiteNo(data) {
      this.listQuery.fpointNo = ''
      this.listQuery.fmmuser = ''
      this.getListfpointNoArea = []
      this.getListfmmuserArea = []
      if (data) {
        // 存放地点
        getListArea({ flag: 10, arg1: data })
          .then(response => {
            this.getListfpointNoArea = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNoArea = []
          })

          // 人员弹出框
        getListArea({ flag: 13, arg1: data })
          .then(response => {
            this.getListfmmuserArea = response.data.data.list
          })
          .catch(() => {
            this.getListfmmuserArea = []
          })
      }
    },

    // 搜索人员弹出框
    searchDialoguse(value) {
      this.userFlag = value
      if (value === 'add') {
        this.userDialogFsiteNo = this.dataForm.fsiteNo
      } else if (value === 'addfcheckuser') {
        this.userDialogFsiteNo = this.dataForm.fsiteNo
      } else {
        this.userDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisibleuse = true
    },

    // 人员弹出框返回值
    userDialogValue(value) {
      if (this.userFlag === 'add') {
        this.dataForm.fmmuser = value.user.id.toString()
      } else if (this.userFlag === 'addfcheckuser') {
        this.dataForm.fcheckuser = value.user.id.toString()
      } else {
        this.listQuery.fmmuser = value.user.id.toString()
      }
    },

    // 搜索地点弹出框
    searchDialoglab(value) {
      this.labFlag = value
      if (value === 'add') {
        this.labDialogFsiteNo = this.dataForm.fsiteNo
      } else {
        this.labDialogFsiteNo = this.listQuery.fsiteNo
      }
      this.searchVisiblelab = true
    },

    // 地点弹出框返回值
    labDialogValue(value) {
      if (this.labFlag === 'add') {
        this.dataForm.fpointNo = value.lab.fpointNo
        this.dataForm.fpointNo = value.lab.fpointNo
        this.dataForm.fequipmentbarcode = null
        this.dataForm.fequipmenttag = null
        this.dataForm.feuipmentid = null
        this.dataForm.fmmlevelno = null
        this.dataForm.details = []
        this.selectList = []
        this.flineList = []
      } else {
        this.listQuery.fpointNo = value.lab.fpointNo
      }
    },
    // 项目
    fmmitemnoFormat: function(row) {
      if (this.getListfmmitemno) {
        for (let i = 0; i < this.getListfmmitemno.length; i++) {
          if (row.fmmitemno == this.getListfmmitemno[i].value) {
            return this.getListfmmitemno[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 保养级别
    formatfmmlevelno: function(row, column) {
      if (this.getListfmmlevelno.length > 0) {
        for (let i = 0; i < this.getListfmmlevelno.length; i++) {
          if (row.fmmlevelno == this.getListfmmlevelno[i].value) {
            return this.getListfmmlevelno[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 保养类型
    formatfmmtypeno: function(row, column) {
      if (this.getListfmmtypeno.length > 0) {
        for (let i = 0; i < this.getListfmmtypeno.length; i++) {
          if (row.fmmtypeno == this.getListfmmtypeno[i].value) {
            return this.getListfmmtypeno[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    // 保养地点
    formatfpointNo: function(row, column) {
      if (this.getListfpointNo) {
        for (let i = 0; i < this.getListfpointNo.length; i++) {
          if (row.fpointNo === this.getListfpointNo[i].value) {
            return this.getListfpointNo[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },

    // 人员
    formatfmmuser: function(row, column) {
      if (this.getListfmmuser) {
        for (let i = 0; i < this.getListfmmuser.length; i++) {
          if (row.fmmuser == this.getListfmmuser[i].value) {
            return this.getListfmmuser[i].label
          }
        }
        return ''
      } else {
        return ''
      }
    },
    formatDate: function(date) {
      if (typeof (date) === 'object') {
        var ym
        var y = date.getFullYear()
        var m = date.getMonth() + 1
        var d = date.getDate()
        if (m < 10) {
          m = '0' + m
        }
        if (d < 10) {
          d = '0' + d
        }
        ym = '' + y + m + d
        return ym
      } else {
        return date
      }
    },
    getList() {
      this.listQuery.ftakedateStart = this.alarmdate == null ? null : this.formatDate(this.alarmdate[0])
      this.listQuery.ftakedateEnd = this.alarmdate == null ? null : this.formatDate(this.alarmdate[1])
      this.listLoading = true
      if (this.listQuery.fbillno == '') {
        this.listQuery.fbillno = null
      }
      listmmHead(this.listQuery)
        .then((response) => {
          const temp = response.data.data.list.filter(item => {
            if (item.fmmlevelno) {
              item.fmmlevelno = item.fmmlevelno.toString()
            }
            if (item.fmmtypeno) {
              item.fmmtypeno = item.fmmtypeno.toString()
            }
            item.feuipmentid = item.feuipmentid.toString()
            return item
          })
          this.list = temp
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        fmmdate: undefined,
        fsiteNo: undefined,
        fpointNo: undefined,
        fequipmentbarcode: undefined,
        fequipmenttag: undefined,
        feuipmentid: undefined,
        fmmtypeno: undefined,
        fmmlevelno: undefined,
        fmmworktime: undefined,
        fmmuser: undefined,
        fexplanation: undefined,
        fupddate: undefined,
        foperator: undefined,
        fcheckdate: undefined,
        fcheckuser: undefined,
        fbillstate: undefined,
        details: [],
        details2: [],
        details3: undefined
      }
    },
    handleUpdate(row) {
      this.actionsTmp = []
      row.details.forEach((v) => {
        this.actionsTmp.push(v)
      })

      this.actionsTmp2 = []
      row.details2.forEach((v) => {
        this.actionsTmp2.push(v)
      })

      if (this.$store.state.user.fsiteLevel != 3) {
        // 存放地点
        getListArea({ flag: 10, arg1: row.fsiteNo })
          .then(response => {
            this.getListfpointNoAreaAdd = response.data.data.list
          })
          .catch(() => {
            this.getListfpointNoAreaAdd = []
          })

        // 人员弹出框
        getListArea({ flag: 13, arg1: row.fsiteNo })
          .then(response => {
            this.getListfmmuserAreaAdd = response.data.data.list
          })
          .catch(() => {
            this.getListfmmuserAreaAdd = []
          })
      }

      this.actionsTmp3 = row.details3
      this.changeFmmitemno(row.feuipmentid, row.fmmlevelno, row.fmmtypeno)
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    changeFmmitemno: function(feuipmentid, fmmlevelno, fmmtypeno) {
      this.getListfmmitemno = []
      if (fmmtypeno) {
        if (feuipmentid) {
          if (fmmlevelno) {
            getListArea({ flag: 27, arg1: feuipmentid, arg2: fmmlevelno })
              .then(response => {
                this.getListfmmitemno = response.data.data.list
              })
              .catch(() => {
                this.getListfmmitemno = []
              })
          } else {
            this.$notify.info({
              message: '暂无保养级别'
            })
          }
        } else {
          this.$notify.info({
            message: '暂无固定资产码'
          })
        }
      } else {
        this.$notify.info({
          message: '暂无巡查类型'
        })
      }
    },

    indexMethod(index) {
      index = index + 1
      return index
    },

    cancel() {
      this.dialogFormVisible = false
      this.list.forEach((v) => {
        if (v.fbillno === this.dataForm.fbillno) {
          this.dataForm.details = this.actionsTmp
          this.dataForm.details2 = this.actionsTmp2
          this.dataForm.details3 = this.actionsTmp3
          const index = this.list.indexOf(v)
          this.list.splice(index, 1, this.dataForm)
          return
        }
      })
    }
  }
}
</script>
